<template>
  <div class="content">
    <!-- <div class="title">料箱机器人&任务列表</div> -->
    <div style="display: flex; height: 100%; gap: 10px">
      <div class="left" :style="style">
        <div class="title">料箱机器人状态</div>
        <div class="small">
          <div class="single">
            <div :style="{ color: data[0].color, fontSize: '8px' }">
              {{ data[0].status }}
            </div>
            <div style="color: white; font-size: 8px">{{ data[0].name }}</div>
            <div>
              <img
                style="width: 40px; height: 40px"
                :src="require('../../../assets/cylinder.png')"
                alt=""
              />
            </div>
          </div>

          <div class="single">
            <div :style="{ color: data[1].color, fontSize: '8px' }">
              {{ data[1].status }}
            </div>
            <div style="color: white; font-size: 8px">{{ data[1].name }}</div>
            <div>
              <img
                style="width: 40px; height: 40px"
                :src="require('../../../assets/cylinder.png')"
                alt=""
              />
            </div>
          </div>
        </div>
        <div class="big">
          <div>
            <img
              style="width: 40px; height: 80px"
              :src="require('../../../assets/big.png')"
              alt=""
            />
          </div>
        </div>
        <div class="small">
          <div class="single">
            <div :style="{ color: data[2].color, fontSize: '8px' }">
              {{ data[2].status }}
            </div>
            <div style="color: white; font-size: 8px">{{ data[2].name }}</div>
            <div>
              <img
                style="width: 40px; height: 40px"
                :src="require('../../../assets/cylinder.png')"
                alt=""
              />
            </div>
          </div>

          <div class="single">
            <div :style="{ color: data[3].color, fontSize: '8px' }">
              {{ data[3].status }}
            </div>
            <div style="color: white; font-size: 8px">{{ data[3].name }}</div>
            <div>
              <img
                style="width: 40px; height: 40px"
                :src="require('../../../assets/cylinder.png')"
                alt=""
              />
            </div>
          </div>
        </div>
      </div>

      <div class="right" :style="style">
        <div class="title" style="margin-top: -10px">检测设备状态</div>
        <div class="r-table">
          <table class="table" width="100%" align="center">
            <thead>
              <tr class="t-head">
                <th style="width: 70%">设备名称</th>
                <th style="width: 30%">状态</th>
              </tr>
            </thead>
            <!-- 滚动区域包裹 -->
            <tbody class="scroll-body">
              <!-- 原始数据 -->
              <tr
                class="tr"
                v-for="(i, index) in tableData"
                :key="'orig-' + index"
              >
                <td style="color: white">
                  <div class="first">{{ i.name }}</div>
                </td>
                <td
                  :style="{
                    color: i.data['在线状态'] === '在线' ? 'green' : 'red',
                  }"
                >
                  {{ i.data["在线状态"] }}
                </td>
              </tr>
              <!-- 复制一份用于无缝滚动 -->
              <tr
                class="tr"
                v-for="(i, index) in tableData"
                :key="'copy-' + index"
              >
                <td style="color: white">
                  <div class="first">{{ i.name }}</div>
                </td>
                <td
                  :style="{
                    color: i.data['在线状态'] === '在线' ? 'green' : 'red',
                  }"
                >
                  {{ i.data["在线状态"] }}
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import { getStateFlag, getOngoingTask } from "@/common/api";
export default {
  name: "RightBottom",
  props: {},
  components: {},
  computed: {
    style() {
      return {
        backgroundImage: `url(${this.bgStyle})`,
        backgroundRepeat: "no-repeat", // 确保背景图片不重复
        backgroundSize: "100% 100%",
      };
    },
  },
  data() {
    return {
      bgStyle: require("@/assets/bgImg.png"),
      data: [
        // { name: "小车1", status: 1 },
        // { name: "小车1", status: 1 },
        // { name: "小车1", status: 1 },
        // { name: "小车1", status: 1 },
      ],
      tableData: [
        // { samNumber: "设备名称", finishStatus: 0 },
        // {
        //   samNumber: "设备名称设备名称设备名称设备名称设备名称",
        //   finishStatus: 0,
        // },
        // {
        //   samNumber: "设备名称设备名称设备名称设备名称设备名称",
        //   finishStatus: 0,
        // },
      ],
      statusArr: [
        {
          label: "移除",
          color: "yellow",
        },
        {
          label: "离线",
          color: "red",
        },
        {
          label: "错误",
          color: "red",
        },
        {
          label: "空闲",
          color: "#4AC7D3",
        },
        {
          label: "搬运中",
          color: "orange",
        },
        {
          label: "等待",
          color: "orange",
        },
      ],
    };
  },
  methods: {
    getStateFlag() {
      getStateFlag().then((res) => {
        if (res.success) {
          for (let i in res.data) {
            let single = this.statusArr.find((j) => j.label == res.data[i]);
            this.data.push({
              name: i,
              status: res.data[i],
              color: single.color || "",
            });
          }
        }
      });
    },
    getOngoingTask() {
      getOngoingTask().then((res) => {
        if (res.success) {
          this.tableData = res.data.data.equipmentBaseData || [];
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
  },
  created() {
    this.getStateFlag();
    this.getOngoingTask();
    setInterval(() => {
      this.getStateFlag();
      this.getOngoingTask();
    }, 1000 * 15);
  },
};
</script>


<style lang="scss" scoped>
.title {
  text-align: center;
  height: 10%;
  color: white;
  font-size: 10px;
}
.content {
  width: 100%;
  height: 100%;

  // .title {
  //   text-align: center;
  //   height: 10%;
  //   color: #00fff4;
  //   font-size: 10px;
  // }

  .left {
    width: 50%;
    height: 100%;
    position: relative;
    .small {
      display: flex;
      align-items: center;
      justify-content: space-around;
      margin-top: 20px;
      .single {
        display: flex;
        flex-direction: column;
        align-items: center;
      }
    }
    .big {
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      left: 86px;
      top: 60px;
    }
  }

  .right {
    width: 47%;
    height: calc(100% - 20px);
    display: flex;
    flex-direction: column;
    padding: 10px;
    .r-table {
      flex: 1;
      overflow-y: auto;
      margin-top: 10px;

      .table {
        table-layout: fixed;
        font-size: 10px;
        color: white;
        border-collapse: collapse;

        .t-head {
          color: #00fff4;
        }

        .tr {
          height: 30px;
          width: 100%;
          .first {
            width: 100%;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            padding-left: 4px;
          }
        }

        tr td {
          text-align: center;
        }
      }
    }
  }
}

thead th {
  position: sticky;
  /* 固定表头 */
  top: 0;
  /* 距离顶部的距离 */
  z-index: 10;
  /* 确保表头在其他内容之上 */
  background-color: #0b38aa;
  color: #56c5ee;
  height: 20px;
}

.r-table::-webkit-scrollbar {
  display: none;
  /* 针对 WebKit 内核浏览器（如 Chrome、Safari） */
}

.r-table {
  -ms-overflow-style: none;
  /* 针对 IE 和 Edge */
  scrollbar-width: none;
  /* 针对 Firefox */
}

/* 偶数行背景色 */
.table tbody tr:nth-child(even) {
  background-color: #092988;
}

/* 奇数行背景色 */
.table tbody tr:nth-child(odd) {
  background-color: #082176;
}

.scroll-body {
  animation: scrollUp 30s linear infinite;
  /* 总动画时间 = 行高 × 行数 ÷ 滚动速度，这里假设每行 20px，10 行 ≈ 200px，20s 较慢 */
}

@keyframes scrollUp {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-50%); /* 滚动到一半（因为复制了一份） */
  }
}
</style>
